Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include DashTx.utils.rpc(basicAuthUrl, method, arg1, arg2, ...) #79

Merged
merged 2 commits into from
Aug 15, 2024

Conversation

coolaj86
Copy link
Member

@coolaj86 coolaj86 commented Aug 15, 2024

No need for insight, DashSight, dashd-rpc, or DashRPC.js.

This is the real deal, right here:

 /**
   * @param {String} basicAuthUrl - ex: https://api:token@trpc.digitalcash.dev/
   *                                    http://user:pass@localhost:19998/
   * @param {String} method - the rpc, such as 'getblockchaininfo',
   *                          'getaddressdeltas', or 'help'
   * @param {...any} params - the arguments for the specific rpc
   *                          ex: rpc(url, 'help', 'getaddressdeltas')
   */
  TxUtils.rpc = async function rpc(basicAuthUrl, method, ...params) {
    let url = new URL(basicAuthUrl);
    let baseUrl = `${url.protocol}//${url.host}${url.pathname}`;
    let basicAuth = btoa(`${url.username}:${url.password}`);

    // typically http://localhost:19998/
    let payload = JSON.stringify({ method, params });
    let resp = await fetch(baseUrl, {
      method: "POST",
      headers: {
        Authorization: `Basic ${basicAuth}`,
        "Content-Type": "application/json",
      },
      body: payload,
    });

    let data = await resp.json();
    if (data.error) {
      let err = new Error(data.error.message);
      Object.assign(err, data.error);
      throw err;
    }

    return data.result;
  };

@coolaj86 coolaj86 merged commit 95fb79a into main Aug 15, 2024
5 checks passed
@coolaj86 coolaj86 deleted the feat-rpc branch August 15, 2024 03:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant